import { createGraphic } from '../../utils/Graphic'
import { BaseLayer } from '../BaseLayer'

export class GraphicLayer extends BaseLayer{
    constructor(options = {}) {
      super(options)
    }

    get layer() {
      let arr = []
      if (this._dataSource) {
        arr.push(this._dataSource)
      }
      if (this._primitiveCollection) {
        arr.push(this._primitiveCollection)
      }
      if (this._pointCollection) {
        arr.push(this._pointCollection)
      }
      if (this._labelCollection) {
        arr.push(this._labelCollection)
      }
      if (this._billboardCollection) {
        arr.push(this._billboardCollection)
      }
      if (this._polylineCollection) {
        arr.push(this._polylineCollection)
      }
      return arr;
    }

    /**
     * 承载Entity类的容器
     *
     * @readonly
     * @memberof GraphicLayer
     */
    get dataSource() {
      if (!this._dataSource) {
        this._dataSource = new Cesium.CustomDataSource(this.uuid)
        this._map.dataSources.add(this._dataSource)
      }
      return this._dataSource
    }

    /**
     * 承载Primitive类的容器
     *
     * @readonly
     * @memberof GraphicLayer
     */
    get primitiveCollection() {
      if (!this._primitiveCollection) {
        this._primitiveCollection = new Cesium.PrimitiveCollection()
        this._map.scene.primitives.add(this._primitiveCollection)
      }
      return this._primitiveCollection
    }

    /**
     * 承载LabelPrimitive类的容器
     *
     * @readonly
     * @memberof GraphicLayer
     */
    get labelCollection() {
      if (!this._labelCollection) {
        this._labelCollection = new Cesium.LabelCollection({scene: this._map.scene})
        this._map.scene.primitives.add(this._labelCollection)
      }
      return this._labelCollection
    }

    /**
     * 承载PointPrimitive类的容器
     *
     * @readonly
     * @memberof GraphicLayer
     */
    get pointCollection() {
      if (!this._pointCollection) {
        this._pointCollection = new Cesium.PointPrimitiveCollection()
        this._map.scene.primitives.add(this._pointCollection)
      }
      return this._pointCollection
    }

    /**
     * 承载BillboardPrimitive类的容器
     *
     * @readonly
     * @memberof GraphicLayer
     */
    get billboardCollection() {
      if (!this._billboardCollection) {
        this._billboardCollection = new Cesium.BillboardCollection({scene: this._map.scene})
        this._map.scene.primitives.add(this._billboardCollection)
      }
      return this._billboardCollection
    }

    /**
     * 承载PolylinePrimitive类的容器
     *
     * @readonly
     * @memberof GraphicLayer
     */
    get polylineCollection() {
      if (!this._polylineCollection) {
        this._polylineCollection = new Cesium.PolylineCollection()
        this._map.scene.primitives.add(this._polylineCollection)
      }
      return this._polylineCollection
    }


    addGraphic(graphic) {
      if (Array.isArray(graphic)) {
        graphic.forEach((item) => {
          this._addGraphic(item)
        })
      } else {
        this._addGraphic(graphic)
      }
      return this
    }

    _addGraphic(graphic) {
      const graphicClass = createGraphic(graphic.type, graphic)
      graphicClass._onAdd(this)
    }
}